﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace 服务端双缓冲队列
{
    class Program
    {

        static long max =200000;
        static void Main(string[] args)
        {

            任意进制转换.Test1();

            Start();
            //TestMethodRunTime(Start);
            Console.Read();
        }


       

        public static void Start()
        {
            var q1 = DoubleBufferingQueue.Create();
            q1.Init(1, 1);
            q1.MaxRun = max;
            var q2 = SingleBufferingQueue.Create();
            q2.Init(1);
            q2.MaxRun = max;

            //双缓冲用时
           Action act=  new Action(() => {
               Task x = Task.Factory.StartNew(q1.DoWork); ;
               DoWorkQ1();
               x.Wait();

           });

           TestMethodRunTime(act, "双缓冲");
           //用时
           Action act2 = new Action(() =>
           {
               线程安全LIst测试 list = 线程安全LIst测试.Create();
               list.MaxRun = max;
               Task x = Task.Factory.StartNew(list.DoWork); ;
               DoWorkQ3();
               x.Wait();
               var dd = 0;
           });

           TestMethodRunTime(act2, "线程安全List");

           //用时
           Action act1 = new Action(() =>
           {
               Task x = Task.Factory.StartNew(q2.DoWork); ;
               DoWorkQ2();
               x.Wait();

           });

           TestMethodRunTime(act1);

        }

        public static void TestMethodRunTime(Action action, string summary="单缓冲")
        {
            Stopwatch sw = new Stopwatch();
            sw.Restart();
            action.Invoke();
            sw.Stop();
            Console.WriteLine(summary+"方法运行时间为：{0}秒",(sw.ElapsedMilliseconds / 1000.0));
        }

        static Random dd = new Random();
        public static void DoWorkQ1()
        {
            int i = 0;
            while (i < max)
            {
                DoubleBufferingQueue.Create().Push(dd.Next(1, 1000000).ToString());
                i++;
            }
        }
        public static void DoWorkQ3()
        {
            int i = 0;
            while (i < max)
            {
                线程安全LIst测试.Create().Push(dd.Next(1, 1000000).ToString());
                i++;
            }
        }
        public static void DoWorkQ2()
        {
            int i = 0;
            while (i < max)
            {
                SingleBufferingQueue.Create().Push(dd.Next(1, 1000000).ToString());
                i++;
            }
        }

    }
}
